﻿using System.Data;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using OfficeOpenXml;
using Simple.Services;
using Simple.Services.System.QHSEDanger.Models.AnalysisInput;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 交叉方违章管理
/// </summary>
[Route("QHSEOverLappingViolation/[action]")]
[ApiController]
public class QHSEOverLappingViolationController : ControllerBase
{
    private readonly QHSEOverLappingViolationService _violationService;
    
    public QHSEOverLappingViolationController(QHSEOverLappingViolationService violationService)
    {
        _violationService = violationService;
    }
    //[HttpPost]
    //[ValidateTimestampAndSignature]
    //public async Task<AppResult> GetBySql([FromBody] IdInputModel model)
    //{
    //    List<QHSEOverLappingViolationModel> data = await _violationService.GetBySqlAsync(model);
    //    return AppResult.Status200OK(data: data);
    //}

    /// <summary>
    /// 违章查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [CustomAuthorize("hdv_qhse_v_all_page")]
    public async Task<AppResult> Page([FromQuery] OverLappingViolationPageInputModel model)
    {
        PageResultModel<QHSEOverLappingViolationModel> data = await _violationService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }
    [HttpGet]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Page1([FromQuery] OverLappingViolationPageInputModel model)
    {
        PageResultModel<QHSEOverLappingViolationModel> data = await _violationService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 新增违章
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_v_all_insert")]
    public async Task<AppResult> Add([FromBody] QHSEOverLappingViolationModel model)
    {
        var i = await _violationService.AddAsync(model);
        return AppResult.Status200OK(data:i);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Add1([FromBody] QHSEOverLappingViolationModel model)
    {
        var i = await _violationService.AddAsync(model);
        if (i == "请完善信息")
        {
            return AppResult.Status500InternalServerError(data: i);
        }
        return AppResult.Status200OK(data: i);
    }
    /// <summary>
    /// 违章编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_v_all_edit")]
    public async Task<AppResult> Edit([FromBody]QHSEOverLappingViolationModel model)
    {
        await _violationService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Edit1([FromBody] QHSEOverLappingViolationModel model)
    {
        await _violationService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 违章删除
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpDelete]
    [CustomAuthorize("hdv_qhse_v_all_delete")]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> model)
    {
        await _violationService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    [HttpDelete]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Delete1([FromBody] List<IdInputModel> model)
    {
        await _violationService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    /// <summary>
    /// 违章详情
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_v_all_querydetail")]
    public async Task<AppResult> QueryDetail([FromBody]IdInputModel inputID)
    {
        var data = await _violationService.QueryDetailAsync(inputID.Id);
        return AppResult.Status200OK(data:data);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> QueryDetail1([FromBody] IdInputModel inputID)
    {
        var data = await _violationService.QueryDetailAsync(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 文件导出
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_v_compage")]
    public async Task<IActionResult> DownloadExcel([FromBody] OverLappingViolationPageInputModel model)
    {
        var fileBytes = await _violationService.UploadExcel(model);
        var fileName = "violation_"+DateTime.Now+".xlsx";
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        return File(fileBytes, contentType, fileName);
    }
    /// <summary>
    /// 统计
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_v_compage")]
    public async Task<AppResult> GetOverLappingTotal([FromBody] OverLappingVadPageInputModel input)
    {
        var data = await _violationService.GetOverLappingTotalAsync(input);
        return AppResult.Status200OK(data: data);
    }
}

